package com.ygxsk.carhome.ui.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.response.BookingInsideBean;
import com.ygxsk.carhome.response.BookingOutsideBean;
import com.ygxsk.carhome.utils.CommUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-09-09  17:14
 * @Description:关于预约的内部的外侧九宫格
 */
public class BookingOutsideAdapter extends BaseQuickAdapter<BookingOutsideBean, BaseViewHolder> {

    //从哪个界面过来的，是正常选择喷涂房过来还是从结算页过来的,有区别
    private int come_from_where;

    public BookingOutsideAdapter(int layoutResId, @Nullable List<BookingOutsideBean> data) {
        super(layoutResId, data);
    }

    public BookingOutsideAdapter(int layoutResId, @Nullable List<BookingOutsideBean> data, int come_from) {
        super(layoutResId, data);
        this.come_from_where = come_from;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, BookingOutsideBean bookingOutsideBean) {

        if (bookingOutsideBean != null) {

            //findview
            TextView textViewHeaderInBooking = (TextView) baseViewHolder.getView(R.id.textviewheaderinbooking);
            //价格的金额描述，选中时间段和该房间的总金额不同；
            TextView textViewTimeCheckEnd = (TextView) baseViewHolder.getView(R.id.textViewTimeCheckEnd);
            TextView textViewTimeCheckPrice = (TextView) baseViewHolder.getView(R.id.textViewTimeCheckPrice);
            //列表内容
            RecyclerView recyclerViewInBookInSideAdapter = (RecyclerView) baseViewHolder.getView(R.id.recyclerviewinbookinsideadapter);
//            String spraySiteId = bookingOutsideBean.getSpraySiteId();

            String siteNo = bookingOutsideBean.getSiteNo();
            //价格信息
            BigDecimal price = bookingOutsideBean.getPrice();

            String priceString = "";

            if (price != null) {
                priceString = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL) + price.toString();
            } else {
                priceString = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL) + CommUtils.formatComma2BigDecimal(0.00).toString();
            }

            //-----------------------------------------------------------------------------------------
            //外层的原始数据
            ArrayList<BookingInsideBean> dataList = bookingOutsideBean.getSiteTimeSpans();
            //没有点击外侧模块的时候就 创建一个空白容器；
//            ArrayList<BookingInsideBean> tempSelectedList_font = new ArrayList<>();

            //filldata

            //标题
            textViewHeaderInBooking.setText(siteNo);

            //该喷涂房间所选时段总金额
            if (come_from_where == ExtraValue.COME_FROM_PAINT_BOOKING_MAIN_ACTIVITY) {
                textViewTimeCheckEnd.setText(StringUtils.getString(R.string.paint_booking_room_unit));
            } else if (come_from_where == ExtraValue.COME_FROM_PAINT_BOOKING_FEE_ACTIVITY) {
                textViewTimeCheckEnd.setText(StringUtils.getString(R.string.paint_booking_room_total));
            }

            //价格
            textViewTimeCheckPrice.setText(priceString);

            //时间的九宫格
            recyclerViewInBookInSideAdapter.setLayoutManager(new GridLayoutManager(getContext(), 3));
            BookingInsideAdapter bookingInsideAdapter = new BookingInsideAdapter(R.layout.bookinginsideadapter, dataList, come_from_where);
            recyclerViewInBookInSideAdapter.setAdapter(bookingInsideAdapter);
            bookingInsideAdapter.notifyDataSetChanged();

            bookingInsideAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                    BookingInsideBean bookingInsideBean = (BookingInsideBean) adapter.getItem(position);
//                  LogUtils.json(ConstantSign.LOGCAT_NOMAL + "bookingInsideBean-->", bookingInsideBean);

                    //如果是选中的就设置不选中：
                    boolean isChecked_Out = bookingInsideBean.isChecked();
                    boolean isUsable = bookingInsideBean.isUsable();
                    //单条数据
                    String timeSpanLabel = bookingInsideBean.getTimeSpanLabel();

                    //----------------------------------------------------------------------------------------------------------------
                    //大前提； 必须是能参与预约；
                    if (isUsable) {

                        //小前提；没有选中，那么点击就是选中
                        if (!isChecked_Out) {

                            //大前提，必须是【能参与预约】
                            for (int indexPosition = 0; indexPosition < dataList.size(); indexPosition++) {


                                if (indexPosition == position) {

//                                    tempSelectedList_font.add(dataList.get(indexPosition));

                                    //大前提，必须是【能参与预约】
                                    if (dataList.get(indexPosition).isUsable()) {
                                        dataList.get(indexPosition).setChecked(true);
                                        bookingInsideAdapter.notifyItemChanged(indexPosition);
                                    }
                                }


                            }

                        } else {

                            for (int indexPosition = 0; indexPosition < dataList.size(); indexPosition++) {

                                if (indexPosition == position) {

//                                    tempSelectedList_font.add(dataList.get(indexPosition));

                                    //大前提，必须是【能参与预约】
                                    if (dataList.get(indexPosition).isUsable()) {
                                        dataList.get(indexPosition).setChecked(false);
                                        bookingInsideAdapter.notifyItemChanged(indexPosition);
                                    }


                                }

                            }

                        }

                    } else {
                        //给个不可预约的消息提示；
                        //如果是喷涂预约的九宫格，那么 "usable": ""是不可预约的提示，
                        //但是，如果是【结算页】过来的,那么点击就是提示【该时间段】
                        //这里有两种情况
                        if (come_from_where == ExtraValue.COME_FROM_PAINT_BOOKING_MAIN_ACTIVITY) {
                            ToastUtils.showShort(StringUtils.getString(R.string.paint_booking_can_not));
                        } else if (come_from_where == ExtraValue.COME_FROM_PAINT_BOOKING_FEE_ACTIVITY) {

//                            ToastUtils.showShort(TextUtils.isEmpty(timeSpanLabel)?timeSpanLabel:"");

                            if (!TextUtils.isEmpty(timeSpanLabel)) {
                                ToastUtils.showShort(timeSpanLabel);
                            }

                        }

                    }

                }
            });

        }
    }
}
